﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParserCore;
using QLabValues;
using AtomicTypes;
using NumericalCalculations;

namespace Memory_Access
{
    public abstract class FunctionImplementation
    {
        
    }

    public class MatlabFunction: FunctionImplementation
    {
        private bool isParsed;
        private string code;
        private Statement parsedFunction;


        /// <summary>
        /// Builds information for an unparsed function.
        /// </summary>
        /// <param name="functionCode"></param>
        public MatlabFunction(string functionCode)
        {
            isParsed = false;
            code = functionCode;
            parsedFunction = null;
        }


        /// <summary>
        /// Builds information for a function which was already parsed.
        /// </summary>
        /// <param name="function"></param>
        public MatlabFunction (FunctionDefinitionStatement function)
        {
            isParsed = true;
            code = null;
            parsedFunction = function;
        }

        public string GetFunctionCode()
        {
            return code;
        }

        public Statement GetFunctionStatement()
        {
            if (!isParsed)
            {
                ParsingEngine commandParser = new ParsingEngine();
                try
                {
                    parsedFunction = commandParser.Parse(code, true);
                }
                catch (ParserException exception)
                {
                    /*OnConsoleEvent("Parser error: " + exception.ParserErrorMessage);
                    return;*/
                }
                /*if (parsedFunction == null)
                    throw new EngineException("Parse failed");*/
            }
            return parsedFunction;
        }

        public bool IsParsed()
        {
            return isParsed;
        }


        /// <summary>
        /// Parses the function. Is called from within the class when needed.
        /// </summary>
        private void Parse()
        {
            if (isParsed)
                return;

            ParsingEngine engine = new ParsingEngine();
            this.parsedFunction = engine.Parse(this.code);
            isParsed = true;
        }
        /*
         * ovaj kod je bio ovde, davno smo radili i neam pojma sta smo s ovim hteli
         * realno, FII treba da sadrzi string sa kodom, flag Parsiran, i drvo, ako je vec parsiran
         * tj sadrzi FunctionDefinitionStatement
        public FunctionTable GetNestedFunctions() // itd.
        {
            // TODO
            return null;
        }

        public FunctionTable GetSubfunctions()
        {
            // TODO
            return null;
        }

        public FunctionTable GetPrivateFunctions()
        {
            // TODO
            return null;
        }

        public FunctionDefinitionStatement GetFunctionStatement()
        {
            // TODO
            return null;
        }

        */
    }

    public class BuiltInFunction : FunctionImplementation
    {
        public Functions.f f;

        public BuiltInFunction(Functions.f f)
        {
            // TODO: Complete member initialization
            this.f = f;
        }
    }
}
